const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    index5: 0,
    nianfen: ['请选择年份','2020','2021'],
    gongzuodidian: ['请选择工作地点'],
    yongrendanwei: ['请选择用人单位'],
    gangweidaima: ['请选择岗位类别'],
    gangweimingcheng: ['请选择岗位名称'],
    phone: 0,
    theList: ''
  },
  bindPickerChange: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('nianfen发送选择改变，携带值为', _.data.nianfen[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=47533&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'nianfen',
        grtext: _.data.nianfen[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择工作地点')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].gongzuodidian)
        }
        _.setData({
          gongzuodidian: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let _ = this;
    console.log('gongzuodidian发送选择改变，携带值为', _.data.gongzuodidian[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=47533&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'gongzuodidian',
        grtext: _.data.gongzuodidian[num2],
        onefiled: 'nianfen',
        onetext: _.data.nianfen[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择用人单位')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].yongrendanwei)

        }
        _.setData({
          yongrendanwei: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let _ = this;
    console.log('yongrendanwei发送选择改变，携带值为', _.data.yongrendanwei[num3]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=47533&callback=?', //接收url
      data: {
        level: '4',
        grfiled: 'yongrendanwei',
        grtext: _.data.yongrendanwei[num3],
        onefiled: 'nianfen',
        onetext: _.data.nianfen[this.data.index],
        twofiled: 'gongzuodidian',
        twotext: _.data.gongzuodidian[this.data.index2],
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        var list = [];
        list.push('请选择岗位代码')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].gangweidaima)
        }
        _.setData({
          gangweidaima: list
        });
      }
    })
  },
  bindPickerChange4: function (e) {
    let num4 = e.detail.value;
    let _ = this;
    console.log('gangweidaima发送选择改变，携带值为', _.data.gangweidaima[num4]);
    this.setData({
      index4: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=47533&callback=?', //接收url
      data: {
        level: '5',
        grfiled: 'gangweidaima',
        grtext: _.data.gangweidaima[num4],
        onefiled: 'nianfen',
        onetext: _.data.nianfen[this.data.index],
        twofiled: 'gongzuodidian',
        twotext: _.data.gongzuodidian[this.data.index2],
        threefiled: 'yongrendanwei',
        threetext: _.data.yongrendanwei[this.data.index3],
      }, //接收参数
      success(res) {
        console.log(res)
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        console.log(theJson.lists)
        var list = [];
        list.push('请选择岗位名称')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].gangweimingcheng)
        }
        _.setData({
          gangweimingcheng: list
        });
      }
    })

  },
  bindPickerChange5: function (e) {
    let num5 = e.detail.value;
    let _ = this;
    console.log('gangweimingcheng', _.data.gangweimingcheng[num5]);
    this.setData({
      index5: e.detail.value
    })
  },

 
  onLoad: function (options) {
    var _ = this;
    var phone = wx.getStorageSync('jmfsxphone')
    console.log(phone)
    if(phone){
      _.setData({
        phone:true
      })
    }
    console.log(options)
    _.setData({
      area:options.area,
      agent:options.agent,
      page:options.page
    })
    
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=47533&callback=?', //接收url
      data: {
        level: '1',
        nianfen: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择年份')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].nianfen)
        }
        _.setData({
          nianfen: list
        });
      }
    })
  },

  ontap: function (e) {
    var _ = this;
    console.log(e.currentTarget.dataset.act)
    var a = parseInt(e.currentTarget.dataset.act)
    this.setData({
      act: a
    })
  },

  tijiao: function (res) {
    var _ = this;
    var nianfen = _.data.nianfen[_.data.index];
    var gongzuodidian = _.data.gongzuodidian[_.data.index2];
    var yongrendanwei = _.data.yongrendanwei[_.data.index3];
    var gangweidaima = _.data.gangweidaima[_.data.index4];
    var gangweimingcheng = _.data.gangweimingcheng[_.data.index5];
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=47533&callback=?",
      data: {
        nianfen: nianfen,
        gongzuodidian: gongzuodidian,
        yongrendanwei: yongrendanwei,
        gangweidaima: gangweidaima,
        gangweimingcheng: gangweimingcheng,
        limits: '100'
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
        if (theJson.status == '2') {
          console.log('暂无数据！')
          wx.showToast({
            title: '暂无数据',
            icon: 'none',
            duration: 2000
          })
        } else {
          _.setData({
            theList: theJson.lists,
            theList1:!1
          })
          wx.pageScrollTo({
            scrollTop:500,
            duration: 300,
          })
        }

        console.log(_.data.theList)
      }
    })
  },
 

  getPhoneNumber(e) {
    var _ = this;
    var area = _.data.area;
    var agent = _.data.uid;
    var page = _.data.page;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      _.setData({
        mobile: res.result,
        phone:!0
      })
      console.log(res.result)
      wx.setStorageSync('jmfsxphone', res.result)
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 44,
          mobile: res.result,
          areaCode: _.data.area,
          agent: _.data.agent,
          pageId: _.data.page
        },
        success: res => {
          console.log(res);
        }
      })
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/register/?actid=47533&callback=?",
        data: {
          phone: res.result,
          area: area,
          agent: agent,
          isagree:true
        }, //接收参数
        success(res) { //返回
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          };
          console.log(theJson)
          
        }
      })
    }).catch(err => {
      console.error(err);
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})